﻿using System.Collections.Generic;
using System;

namespace KMLHelper
{
    public class KMLPolygon : KMLGeometry
    {
        private List<KMLVector3> coords = null;
        /// <summary>
        /// 坐标集合
        /// </summary>
        public List<KMLVector3> Coords
        {
            set {
                this.coords = value;

                //闭合面对象
                if (this.coords != null && this.coords.Count > 0)
                {
                    if (this.coords[this.coords.Count - 1] != this.coords[0])
                    {
                        this.coords.Add(new KMLVector3(this.coords[0]));
                    }
                }
            }
            get { return this.coords; }
        }

        private string begin = null;
        /// <summary>
        /// 轨迹的开始时间
        /// </summary>
        public string TimeBeginStr
        {
            set
            {
                DateTime time = DateTime.Now;
                if (DateTime.TryParse(value, out time))
                {
                    this.begin = time.ToString(KMLConstant.TimeFormat);
                }
            }
        }
        /// <summary>
        /// 轨迹的开始时间
        /// </summary>
        public DateTime TimeBegin
        {
            set
            {
                this.begin = value.ToString(KMLConstant.TimeFormat);
            }
        }

        private string end = null;
        /// <summary>
        /// 轨迹的结束时间
        /// </summary>
        public string TimeEndStr
        {
            set
            {
                DateTime time = DateTime.Now;
                if (DateTime.TryParse(value, out time))
                {
                    this.end = time.ToString(KMLConstant.TimeFormat);
                }
            }
        }
        /// <summary>
        /// 轨迹的结束时间
        /// </summary>
        public DateTime TimeEnd
        {
            set
            {
                this.end = value.ToString(KMLConstant.TimeFormat);
            }
        }

        public KMLPolygon()
        {
            this.geoType = EnumKMLGeoType.Polygon;
        }

        public override void Write(System.IO.StreamWriter sw)
        {
            try
            {
                if (this.coords != null && this.coords.Count > 0)
                {
                    if (string.IsNullOrEmpty(this.begin) == false ||
                        string.IsNullOrEmpty(this.end) == false)
                    {
                        sw.WriteLine("<TimePeriod>");
                        if (string.IsNullOrEmpty(this.begin) == false)
                        {
                            sw.WriteLine("<begin>");
                            sw.WriteLine("<TimeInstant>");
                            sw.WriteLine("<timePosition>" + begin + "</timePosition>");
                            sw.WriteLine("</TimeInstant>");
                            sw.WriteLine("</begin>");
                        }
                        if (string.IsNullOrEmpty(this.end) == false)
                        {
                            sw.WriteLine("<end>");
                            sw.WriteLine("<TimeInstant>");
                            sw.WriteLine("<timePosition>" + end + "</timePosition>");
                            sw.WriteLine("</TimeInstant>");
                            sw.WriteLine("</end>");
                        }
                        sw.WriteLine("</TimePeriod>");
                    }

                    sw.WriteLine("<Polygon>");
                    sw.WriteLine("<outerBoundaryIs>");
                    sw.WriteLine("<LinearRing>");
                    sw.WriteLine("<coordinates>");

                    foreach (KMLVector3 pt in this.coords)
                    {
                        sw.Write(pt.Longitude + "," + pt.Latitude + "," + pt.Elevation + " ");
                    }

                    sw.WriteLine("</coordinates>");
                    sw.WriteLine("</LinearRing>");
                    sw.WriteLine("</outerBoundaryIs>");
                    sw.WriteLine("</Polygon>");
                }
            }
            catch
            { throw; }
        }
    }
}
